package com.STS.ELearning.DAO
{
	import com.STS.ELearning.VO.AyarlarVO;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.errors.SQLError;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	
	
	public class Ayarlar extends BaseDao
	{
		private static var instance:Ayarlar;
		private static const TABLE_NAME:String="Ayarlar";
		public static function getInstance():Ayarlar {
			if( instance == null )
				instance = new Ayarlar( new SingletonLock );
			return instance;
		}
		
		
		public function Ayarlar( lock: SingletonLock) {
			
			super(TABLE_NAME);
		}
		public override function getTableContent(order:String=null, where:String=null, whereParams:Array=null):Array{
			var data:Array=super.getTableContent(null,where,whereParams);
			if(data != null && data.length>0)
			{
				var arrl:Array=new Array();
				for each(var item:Object in data)
				{
					var ay:AyarlarVO=new AyarlarVO();
					ay.ad=item.ad;
					ay.grup=item.grup;
					ay.deger=item.deger;
					arrl.push(ay);
				}
				return arrl;
			}	
			return null;
		}
		public function getAyarByGrup( gr:String ):Array {
			var text:String = ' (grup=@grup) ';
			var parameters:Array=[{name:"grup",value:gr}];
			return getTableContent(null,text,parameters);
		}
		public function getAyarByAd( gr:String,ad:String ):AyarlarVO {
			var text:String = ' (grup=@grup and ad=@ad) ';
			var parameters:Array=[{name:"grup",value:gr},{name:"ad",value:ad}];
			var arr:Array= getTableContent(null,text,parameters);
			if(arr!=null && arr.length>0)
				return arr[0] as AyarlarVO;
			return null;
		}
		public function insertRow( rowItem:Object):Boolean {
			var text:String = 'INSERT INTO main.'+TABLE_NAME+'(grup,ad,deger) VALUES (@grup,@ad,@deger)';
			var params:Array = [{name:"grup", value:rowItem.grup},{name:"ad", value:rowItem.ad}, {name:"deger", value:rowItem.deger} ];
			return executeSingleResult(text,params);
			
		}
		public function updateRow( rowItem:Object):Boolean {
			var text:String = 'UPDATE main.'+TABLE_NAME+' SET deger = @deger WHERE  grup = @grup and ad = @ad';
			var params:Array=[ {name:"grup", value:rowItem.grup}, {name:"ad", value:rowItem.ad}, {name:"deger", value:rowItem.deger}] ;
			return executeSingleResult(text,params);
		}
				
		public function deleteRow( rowItem:Object):Boolean {
			var text:String = 'DELETE FROM main.'+TABLE_NAME+' WHERE grup=@grup and ad = @ad;';
			var params:Array = [ {name:"grup", value:rowItem.grup}, {name:"ad", value:rowItem.ad} ];
			return executeSingleResult(text,params);
		}
		public function AyarCek(gr:String,ad:String,deger:String):void
		{
			var ayar:AyarlarVO=new AyarlarVO();
			ayar.grup=gr;
			ayar.ad=ad;
			ayar.deger=deger;
			if(!this.updateRow(ayar))
			{
				this.insertRow(ayar);
			}
			ayar=null;
		}
	}
}
class SingletonLock {}